package com.morkalork.swing;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.Console;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.morkalork.entities.Brick;

public class Board extends JPanel implements MouseListener
{
	private final String BACKGROUND_URL = "/resources/Background.png";
	private final String BRICK_AREA_URL = "/resources/BrickArea.png";
	private final String CONSOLE_URL = "/resources/Console.png";
	private final String GAME_INFO_URL = "/resources/GameInfo.png";
	private final String EMPTY_LIGHT_URL = "/resources/EmptyLight.png";
	
	private BrickManager m_BrickManager;
	
	public Board()
	{
		m_BrickManager = new BrickManager();
		addMouseListener(this);
	}
	
	public void paint(Graphics g)
	{
		Graphics2D d2g = (Graphics2D)g;	
		setLayout(null);
		buildBackground(d2g);
		addBrickArea(d2g);
		addBricks(d2g);
		addConsole(d2g);
		addGameInformation(d2g);
		addEmptyLights(d2g);
	}
	
	private void buildBackground(Graphics2D g)
	{
		ImageIcon icon = new ImageIcon(getClass().getResource(BACKGROUND_URL));
		g.drawImage(icon.getImage(), 0, 0, null);
	}
	
	private void addEmptyLights(Graphics2D g)
	{
		int xOffSet = 40;
		int yOffSet = 320;
		int width = 60;
		
		for(int i = 0; i < 5; i++)
		{
			ImageIcon icon = new ImageIcon(getClass().getResource(EMPTY_LIGHT_URL));
			g.drawImage(icon.getImage(), xOffSet + (i * width), yOffSet, null);
		}
	}
	
	private void addConsole(Graphics2D g)
	{
		ImageIcon icon = new ImageIcon(getClass().getResource(CONSOLE_URL));
		g.drawImage(icon.getImage(), 290, 114, null);
	}
	
	private void addGameInformation(Graphics2D g)
	{
		ImageIcon icon= new ImageIcon(getClass().getResource(GAME_INFO_URL));
		g.drawImage(icon.getImage(), 300, 50, null);
	}
	
	private void addBrickArea(Graphics2D g)
	{
		ImageIcon icon = new ImageIcon(getClass().getResource(BRICK_AREA_URL));
		g.drawImage(icon.getImage(), 50, 50, null);
	}
	
	private void addBricks(Graphics2D g)
	{
		for(Brick brick : m_BrickManager.getBricks())
		{
			ImageIcon icon = new ImageIcon(getClass().getResource(brick.getColorUrl()));
			Image brickImage = icon.getImage();
			g.drawImage(brickImage, (int)brick.getCurrentPoint().getX(), (int)brick.getCurrentPoint().getY(), null);
		}
	}
	
	public void mousePressed(MouseEvent e) 
	{
    }

    public void mouseReleased(MouseEvent e) 
    {
    }

    public void mouseEntered(MouseEvent e) 
    {
    }

    public void mouseExited(MouseEvent e) 
    {
    }

    public void mouseClicked(MouseEvent e) 
    {
    	int x = e.getX();
    	int y = e.getY();
    	
    	if((x > 70 && y > 70) &&
    		(x < 270 && y < 270))
    	{
    		for(Brick brick : m_BrickManager.getBricks())
    		{
    			if((brick.getCurrentPoint().getX() < x && (brick.getCurrentPoint().getX() + 20) > x) &&
    					brick.getCurrentPoint().getY() < y && (brick.getCurrentPoint().getY() + 20) > y)
    			{
    				m_BrickManager.flood(brick);
    				m_BrickManager.getBricks().remove(brick);
    				break;
    			}
    		}
    	}
    	
    	repaint();
    }
}
